package Cau10;

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class BlackBoardClient {

	public static class displayPanel extends JPanel implements MouseListener,
			MouseMotionListener {
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		Line line = new Line();
		DatagramSocket socket;
		DatagramPacket packet;
		InetAddress ia;
		// bai2_pointObject p = new bai2_pointObject();

		JTextField txtsize = new JTextField("2", 4);
		JTextField txtcolorRed = new JTextField("0", 4);
		JTextField txtcolorGreen = new JTextField("0", 4);
		JTextField txtcolorblack = new JTextField("0", 4);

		JLabel size = new JLabel("size");
		JLabel Red = new JLabel("Red");
		JLabel Green = new JLabel("Green");
		JLabel Black = new JLabel("Black");

		public displayPanel() {
			// Design form
			this.setSize(800, 550);
			this.add(size);
			this.add(txtsize);
			this.add(Red);
			this.add(txtcolorRed);
			this.add(Green);
			this.add(txtcolorGreen);
			this.add(Black);
			this.add(txtcolorblack);
			this.setLayout(new FlowLayout());

			addMouseListener(this);
			addMouseMotionListener(this);

			try {
				socket = new DatagramSocket();
				ia = InetAddress.getLocalHost();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}

		public void writeObjectToSocket(Line line) {
			try {
				ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
				ObjectOutputStream os = new ObjectOutputStream(byteStream);
				os.writeObject(line);
				packet = new DatagramPacket(byteStream.toByteArray(),
						byteStream.size(), ia, 5000);
				socket.send(packet);

			} catch (IOException e) {
				e.printStackTrace();

			}
		}

		public Color setcolor() {
			if (txtcolorGreen.getText() == "" || txtcolorblack.getText() == ""
					|| txtcolorRed.getText() == "") {
				return Color.BLACK;
			} else {
				int _red = Integer.parseInt(txtcolorRed.getText());
				int _green = Integer.parseInt(txtcolorGreen.getText());
				int _black = Integer.parseInt(txtcolorblack.getText());
				return new Color(_red, _green, _black);
			}
		}

		public String setSize() {
			if (txtsize.getText() == "") {
				return "2";
			} else {
				return txtsize.getText();
			}
		}

		@Override
		public void mousePressed(MouseEvent e) {
			// Lay diem dau
			this.line.set_sPoint(new Point(e.getX(), e.getY()));
			// Xem diem dau cung la diem cuoi
			this.line.set_ePoint(this.line.get_sPoint());
		}

		@Override
		public void mouseDragged(MouseEvent e) {
			line.setColor(setcolor());
			line.setSize(Float.parseFloat(setSize()));
			
			Graphics2D g = (Graphics2D) getGraphics();

			BasicStroke stroke = new BasicStroke(
					(float) Integer.parseInt(txtsize.getText()));
			g.setStroke(stroke);
			g.setColor(setcolor());
			g.setPaintMode();
			g.setXORMode(getBackground());
			
			g.drawLine(this.line.get_sPoint().x, this.line.get_sPoint().y,
					this.line.get_ePoint().x, this.line.get_ePoint().y);
			
			// Lay diem dang xet gan cho ePoint
			this.line.set_ePoint(new Point(e.getX(), e.getY()));

			g.drawLine(this.line.get_sPoint().x, this.line.get_sPoint().y,
					this.line.get_ePoint().x, this.line.get_ePoint().y);

		}

		@Override
		public void mouseMoved(MouseEvent e) {

		}

		@Override
		public void mouseClicked(MouseEvent arg0) {

		}

		@Override
		public void mouseEntered(MouseEvent arg0) {

		}

		@Override
		public void mouseExited(MouseEvent e) {
			
		}

		@Override
		public void mouseReleased(MouseEvent e) {
			this.line.set_ePoint(new Point(e.getX(), e.getY()));
			Graphics g = getGraphics();
			g.setPaintMode();
			g.drawLine(this.line.get_sPoint().x, this.line.get_sPoint().y,
					this.line.get_ePoint().x, this.line.get_ePoint().y);
			writeObjectToSocket(line);
		}
	}

	public static void main(String[] args) {
		JFrame win = new JFrame("Blackboar Client");
		while (win != null && (win instanceof JFrame) == false) {
			win = (JFrame) win.getParent();
		}
		if (win instanceof JFrame) {
			win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			win.setSize(800, 600);
			win.setVisible(true);
			displayPanel aPanel = new displayPanel();
			win.setContentPane(aPanel);
		}
	}

}
